<?php
/**
 * Created by PhpStorm.
 * User: sosyuki
 * Date: 2018/9/30
 * Time: 14:49
 */

namespace Drupal\views_echarts\ViewsController\handler\plugin;

use views_plugin_style;

class ViewsPluginStyleECharts extends views_plugin_style {

  public function render() {
    $view_name = $this->view->name;
    $current_display = $this->view->current_display;
    $results = $this->view->style_plugin->render_fields($this->view->result);

    $data = [];
    foreach ($results as $result) {

    }

    $viewsEcharts = [];
    $viewsEcharts[$this->view->name . '-' . $this->view->current_display] = [
      'view_name' => $this->view->name,
      'view_display_id' => $this->view->current_display,
      'option' => [
        'title' => ['text' => $this->view->get_title()],
        'tooltip' => [
          'show' => TRUE,
          "trigger" => 'axis',
        ],
        'legend' => [
          'data' => ["销量", "访问量"],
        ],
        'xAxis' => [
          'data' => ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        ],
        'yAxis' => [
          //          ['data' => ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]],
          'show' => TRUE,
        ],
        'series' => [
          [
            'name' => '销量',
            'type' => 'line',
            'data' => [5, 20, 36, 10, 10, 20],
          ],
          [
            'name' => '访问量',
            'type' => 'line',
            'data' => [5 + 3, 20 + 1, 36 + 5, 10, 10, 20],
          ],
        ],
      ],
    ];
    drupal_add_js(['viewsEcharts' => $viewsEcharts], 'setting');

    return <<<tpl
<div id="echart-main-{$view_name}-{$current_display}" style="width: 600px;height:400px;"></div>'
tpl;
  }

}
